//
//  PicPikerCollectionView.swift
//  weibo
//
//  Created by 叶子韬韬 on 2017/5/8.
//  Copyright © 2017年 叶子韬韬. All rights reserved.
//

import UIKit
private let picPikercell = "picPikercell"
private let edgeMargin : CGFloat = 15
class PicPikerCollectionView: UICollectionView {

    // MARK:- #注释>定义数组
    var images:[UIImage] = [UIImage](){
        didSet{
            reloadData()
        }
    }
    
    
    override func awakeFromNib() {
        super.awakeFromNib()
        
        //设置UICollectionView layout
        let layout = collectionViewLayout as! UICollectionViewFlowLayout
        
        let itemWH = (UIScreen.main.bounds.width - 4*edgeMargin)/3
        
        
        layout.itemSize = CGSize.init(width: itemWH, height: itemWH)
        layout.minimumInteritemSpacing = edgeMargin
        layout.minimumLineSpacing = edgeMargin
        
        
        register(UINib.init(nibName: "PicPikerViewCell", bundle: nil), forCellWithReuseIdentifier: picPikercell)
        
        dataSource = self
        //设置collectionView的内边距
        contentInset = UIEdgeInsets(top: edgeMargin, left: edgeMargin, bottom: edgeMargin, right: edgeMargin)
    }

}
extension PicPikerCollectionView:UICollectionViewDataSource{
   
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return images.count + 1
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        //创建cell
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: picPikercell, for: indexPath) as! PicPikerViewCell
        
        cell.image = indexPath.item <= images.count-1 ? images[indexPath.item] : nil
        
        
        return cell
    }
}


